const {
  $Message
} = require('../../iview/base/index');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    picUploadFinish: false
  },

  inputChange(e) {
    var that = this
    switch (e.currentTarget.id) {
      case "message": {
        that.setData({
          message: e.detail.value
        })
      }
      break
    case "profile": {
      that.setData({
        profile: e.detail.value
      })
    }
    break
    case "place": {
      that.setData({
        place: e.detail.value
      })
    }
    break
    }
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  uploadPic: function (e) {
    var that = this
    if (this.data.message == undefined) {
      $Message({
        content: '请输入内容',
        type: 'default'
      })
    } else {
      wx.chooseImage({
        success: function (img) {
          wx.cloud.uploadFile({
            cloudPath: "eventimage/" + that.data.message + ".png",
            filePath: img.tempFilePaths[0]
          }).then(function (cldimg) {
            console.log(cldimg)
            that.setData({
              imgPath: cldimg.fileID,
              picUploadFinish: true
            })
          })
        }
      })
    }
  },

  commit: function (e) {
    var that = this
    if (this.data.message != undefined && this.data.imgPath != undefined) {
      var _post = {
        message: that.data.message,
        picture: that.data.imgPath,
        count: 0
      }

      wx.cloud.callFunction({
        name: "postMessage",
        data: {
          postobj: _post
        }
      }).then(function (e) {
        console.log(e)
        $Message({
          content: '动态发布成功',
          type: "success"
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1,
          })
        }, 500)

      }).catch(function (err) {
        $Message({
          content: '动态发布失败',
          type: 'error'
        })
      })
    } else {
      $Message({
        content: '请填写完整信息喔~',
        type: "defalut"
      });
    }

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})